package com.yunhoo.base.bean;

import com.yunhoo.base.db.impl.InventImpl;
import com.yunhoo.base.model.Invent;
import com.yunhoo.base.model.InventType;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.richfaces.event.TreeSelectionChangeEvent;

@ManagedBean(name = "inventBean")
@SessionScoped
public class InventBean implements Serializable {

    private Invent dataInput = null;
    private List<Invent> dataList = null;
    private InventType inventType = null;//存货类型
    //
    private boolean addOrUpdate = false;
    private boolean editStatus = false;

    public InventBean() {
    }

    private void initStatus() {
        addOrUpdate = false;
        editStatus = false;
    }

    public void queryli() {
        initList(this.inventType.getCode());
        initStatus();
        dataInput = null;
    }

    private void initList(String inventTypeCode) {
        dataList = InventImpl.list(inventTypeCode);
    }

    public void selected(int rowIndex) {
        dataInput = dataList.get(rowIndex);
        System.out.println("orgCode = " + dataInput.getOrgCode());
        initStatus();
    }

    public void treeSelected(TreeSelectionChangeEvent event) {
        InventType temp = (InventType) TreeUtil.listener(event);

        if (inventType == null) inventType = new InventType(true);

        if (inventType.getCode() != null && temp.getCode().trim().equals(inventType.getCode().trim())) {
            return;
        }

        inventType.setCode(temp.getCode().trim());
        inventType.setName(temp.getName());

        initStatus();
        
        dataInput = null;
        initList(inventType.getCode());
    }

    public void add() {
        if (null == inventType) return;

        addOrUpdate = true;
        editStatus = true;
        dataInput = new Invent();
        dataInput.setType(inventType);

    }

    public void update() {
        addOrUpdate = false;
        editStatus = true;
    }

    public void delete() {
        boolean ok = InventImpl.delete(dataInput);
        if (ok) {
            initStatus();
            initList(inventType.getCode());
            dataInput = null;
            ToolKit.msgs_info("已删除！");
        } else {
            ToolKit.msgs_err("删除失败！");
        }
    }

    public void save() {
        boolean ok;
        if (addOrUpdate) {

            ok = InventImpl.insert(dataInput);

        } else {
            ok = InventImpl.update(dataInput);
        }
        if (ok) {
            initStatus();
            initList(inventType.getCode());
            ToolKit.msgs_info("已保存！");
        } else {
            ToolKit.msgs_err("保存失败！");
        }
    }

    public void cancel() {
        if (addOrUpdate) {
            dataInput = null;
        }
        initStatus();
    }

    public List<Invent> getDataList() {
//        if (dataList == null && inventType != null) {
//            initList(inventType.getCode());
//        }
        return dataList;
    }

    public void setDataList(List<Invent> dataList) {
        this.dataList = dataList;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public Invent getDataInput() {
        return dataInput;
    }

    public void setDataInput(Invent dataInput) {
        this.dataInput = dataInput;
    }

    public InventType getInventType() {
        return inventType;
    }

    public void setInventType(InventType inventType) {
        this.inventType = inventType;
    }
}
